package com.solocredit.finance.mvp.presenter

import android.content.Context
import com.solocredit.finance.mvp.view.MvpView
import java.lang.ref.WeakReference

/**
 * MullaFlow
 *
 * @Author wwdeng
 * @CreateTime: 2024/7/24 15:09
 * @Annotation：
 */
open class MvpPresenter<V : MvpView>(context: Context?) : MvpBasePresenter<V> {
    private var weakView: WeakReference<V>? = null
    private var weakContext: WeakReference<Context> = WeakReference(context)

    val context: Context?
        get() = weakContext.get()

    override fun attachView(view: V) {
        this.weakView = WeakReference(view)
    }

    /**
     * 获取view对象
     * @return 返回当前MvpView
     */
    val mvpView: V?
        get() = weakView?.get()

    override fun detachView() {
        weakView?.clear()
        weakView = null
    }

    /**
     * 是否是网络层错误
     */
    fun isNetworkError(errorCode: Int): Boolean {
        //-1 网络连接失败
        //408 请求超时
        //500..599 服务器处理错误
        return errorCode == -1 || errorCode == 408 || (errorCode >= 500 && errorCode <= 599)
    }
}
